package com.newland.mesdk.demo.fragment;

import java.io.File;

import android.app.Fragment;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.newland.mesdk.demo.main.R;
import com.newland.mtype.module.common.printer.FontSettingScope;
import com.newland.mtype.module.common.printer.FontType;
import com.newland.mtype.module.common.printer.LiteralType;
import com.newland.mtype.module.common.printer.PrinterResult;
import com.newland.mtype.module.common.printer.PrinterStatus;
import com.newland.mtype.module.common.printer.ThrowType;
import com.newland.mesdk.demo.interfaceimpl.PrinterInterfaceImpl;
import com.newland.mesdk.demo.main.MainActivity;
import com.newland.mesdk.demo.util.Const.MessageTag;

/**
 * Created by YJF 打印模块
 */
public class PrinterFragment extends Fragment implements OnClickListener {
	private MainActivity mainActivity;
	private PrinterInterfaceImpl printerInterfaceImpl;
	private Button btnGetState, btnInit, btnPrintPic, btnPrintScrip;
	private Button btnPrintStr, btnSetDensity, btnSetFont, btnSetLinSp;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_printer, null);
		mainActivity = (MainActivity) getActivity();
		initPrinterFragment(view);
		return view;
	}

	public void initPrinterFragment(View view) {
		btnGetState = (Button) view.findViewById(R.id.btn_printer_getstate);
		btnGetState.setOnClickListener(this);

		btnInit = (Button) view.findViewById(R.id.btn_printer_init);
		btnInit.setOnClickListener(this);

		btnPrintPic = (Button) view.findViewById(R.id.btn_printer_print_pic);
		btnPrintPic.setOnClickListener(this);

		btnPrintScrip = (Button) view.findViewById(R.id.btn_printer_print_script);
		btnPrintScrip.setOnClickListener(this);

		btnPrintStr = (Button) view.findViewById(R.id.btn_printer_print_string);
		btnPrintStr.setOnClickListener(this);

		btnSetDensity = (Button) view.findViewById(R.id.btn_printer_set_density);
		btnSetDensity.setOnClickListener(this);

		btnSetFont = (Button) view.findViewById(R.id.btn_printer_set_font);
		btnSetFont.setOnClickListener(this);

		btnSetLinSp = (Button) view.findViewById(R.id.btn_printer_set_linespace);
		btnSetLinSp.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (mainActivity.isDeviceInit()) {
			if (mainActivity.processingisLocked()) {
				mainActivity.showMessage("请先完成或撤销当前操作...", MessageTag.ERROR);
			} else {
				mainActivity.connectDevice();
				printerInterfaceImpl=new PrinterInterfaceImpl();
				switch (v.getId()) {
				case R.id.btn_printer_getstate:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							mainActivity.showMessage("打印机状态：" + printerInterfaceImpl.getStatus() + "\r\n", MessageTag.DATA);
							mainActivity.processingUnLock();

						}
					}).start();

					break;
				case R.id.btn_printer_init:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							printerInterfaceImpl.init();
							mainActivity.showMessage("打印机初始化成功" + "\r\n", MessageTag.NORMAL);
							mainActivity.processingUnLock();
						}
					}).start();

					break;
				case R.id.btn_printer_set_font:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							printerInterfaceImpl.setFontType(LiteralType.CHINESE, FontSettingScope.HEIGHT, FontType.DOUBLE);
							mainActivity.showMessage("设置字体成功！" + "\r\n", MessageTag.NORMAL);
							mainActivity.showMessage("设置字体为：中文的高为双倍" + "\r\n", MessageTag.DATA);
							mainActivity.processingUnLock();

						}
					}).start();

					break;
				case R.id.btn_printer_set_linespace:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							printerInterfaceImpl.setLineSpace(3);
							mainActivity.showMessage("行间隔设置成功！" + "\r\n", MessageTag.NORMAL);
							mainActivity.showMessage("行间隔设置为3" + "\r\n", MessageTag.DATA);
							mainActivity.processingUnLock();

						}
					}).start();

					break;
				case R.id.btn_printer_set_density:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							printerInterfaceImpl.setDensity(12);
							mainActivity.showMessage("浓度设置成功！" + "\r\n", MessageTag.NORMAL);
							mainActivity.showMessage("浓度设置为12" + "\r\n", MessageTag.DATA);
							mainActivity.processingUnLock();
						}
					}).start();

					break;
				case R.id.btn_printer_print_string:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							if (printerInterfaceImpl.getStatus() != PrinterStatus.NORMAL) {
								mainActivity.showMessage("打印失败！打印机状态不正常" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							} else {
								mainActivity.showMessage("打印中......" + "\r\n", MessageTag.NORMAL);
								String bill = "\n\n    " + "      xxPOS签购单" + "\n";// 交易明细信息
								bill += "商户名称(MERCHANT NAME)：钓鱼岛\n";
								bill += "商户编号(MERCHANT NO.)：805320000000002\n";
								bill += "终端编号(TERMINAL NO.)：80532021\n";
								bill += "操作员号(OPERATOR NO.)：001\n";
								bill += "卡类型：VISA\n";
								bill += "卡号(CARD NO.)：6222222222222222222\n";
								bill += "消费类型：消费\n";
								bill += "授权号(AUTH NO.)：000001\n";
								bill += "参考号(REFER NO.)：000000000000001\n";
								bill += "日期时间(DATE/TIME)：2014-03-24\n";
								bill += "交易金额(AMOUNT)：100RMB\n";
								bill += "---------------------------------------\n";
								bill += "本人确认以上交易，同意将其记入本卡账户\n";
								bill += "I ACKNOWLEDGE SATISFATORY RECEIPT OF RELATIVE GOODS/SERVICES\n";
								bill += "VFI-VX520-31990301 商户存根/MERCHANT COPY\n";
								bill += "----------x-----------------x----------\n\n\n\n\n";
								PrinterResult printerResult = printerInterfaceImpl.printString(bill);
								mainActivity.showMessage("打印结果：" + printerResult.toString() + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();
							}
						}
					}).start();

					break;
				case R.id.btn_printer_print_pic:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							if (printerInterfaceImpl.getStatus() != PrinterStatus.NORMAL) {
								mainActivity.showMessage("打印失败!打印机状态不正常" + "\r\n", MessageTag.ERROR);
							} else {
								mainActivity.showMessage("图片打印中......" + "\r\n", MessageTag.TIP);
//								BitmapFactory.Options bfoOptions = new BitmapFactory.Options();
//								bfoOptions.inScaled = false;
//								final Bitmap bitmap =BitmapFactory.decodeResource( getResources(), R.drawable.ic_launcher, bfoOptions );
								Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
								printerInterfaceImpl.printBitMap(0, bitmap);
								printerInterfaceImpl.paperThrow(ThrowType.BY_LINE,2);//走紙
								mainActivity.showMessage("图片打印成功!" + "\r\n", MessageTag.NORMAL);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;
				case R.id.btn_printer_print_script:
					new Thread(new Runnable() {

						@Override
						public void run() {
							mainActivity.processingLock();
							if (printerInterfaceImpl.getStatus() != PrinterStatus.NORMAL) {
								mainActivity.showMessage("打印失败！打印机状态不正常" + "\r\n", MessageTag.ERROR);
								mainActivity.processingUnLock();
							} else {
								mainActivity.showMessage("脚本打印中......" + "\r\n", MessageTag.TIP);
								printerInterfaceImpl
										.printScript("!hz l\n !asc sl\n *feedline 3\n *image c 384*71 #ums\n *feedline 1\n *line\n *text l 终端编号(TERMINAL NO.):\n *text l 00011001130000490868\n *text l 操作员号(OPERATOR NO.):\n *text l 01\n *text l 发卡行号(ISSUER NO.):\n *text l 00001\n *text l 收单行号(ACQUIRER NO.):\n *text l 00002\n *text l 有效期(EXP.DATE):\n *text l 2022/08\n *text l 卡号(CARD NO.):\n *text l 622821060028802716\n *text l 交易类型(TRANS TYPE):\n *text l SALE\n *text l 批次号(BATCH NO.):\n *text l 000001\n *text l 凭证号(VOUCHEE NO.):\n *text l 938302\n *text l 授权码(AUTH NO.):\n *text l 012345\n *text l 参考号(REFER NO.):\n *text l 190512928302\n *text l 日期时间(DATE/TIME):\n *text l 2014-06-09 19:05:12\n *text l 金额(AMOUNT):\n !gray 10\n !asc l\n *text l RMB:0.20\n !gray 5\n !asc sl\n *text l 备注(REFERENCE):\n *feedline 3\n *line\n !hz s\n !asc s\n *text l 服务热线：95534\n *text l PAX-P80-311002 持卡人存根CARDHOLDER COPY");
								mainActivity.showMessage("脚本打印成功！" + "\r\n", MessageTag.DATA);
								mainActivity.processingUnLock();
							}

						}
					}).start();

					break;

				default:
					break;
				}
			}
		} else {
			mainActivity.showMessage("设备未初始化，请先初始化！" + "\r\n", MessageTag.TIP);
		}
	}

	
}
